/*
 * Copyright 2009-2010 Nanjing RedOrange ltd (http://www.red-orange.cn)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package redora.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.datepicker.client.DatePicker;

import java.util.Date;

import static com.google.gwt.dom.client.Style.Cursor.POINTER;
import static com.google.gwt.user.client.ui.HasHorizontalAlignment.ALIGN_LEFT;

/**
 * Data bound Date and DateTime input box. When the widget is enabled
 * a calendar button is displayed. Clicked it shows a Data Picker popup.
 * The user can also input the date manually through the text box. Input is validated.
 * @author Nanjing RedOrange (www.red-orange.cn)
 */
public class DateBox extends Composite implements HasName, HasValue<Date> {

    static RedoraResource resources = GWT.create(RedoraResource.class);

    final Grid datePanel;
    final TextBox date;
    final Image calendar;
    final DateTimeFormat format;

    public DateBox(String name, String id, DateTimeFormat format) {
        this.format = format;
        datePanel = new Grid(1, 2);
        datePanel.setCellPadding(0);
        datePanel.setCellSpacing(0);
        initWidget(datePanel);
        date = new TextBox();
        date.setEnabled(isEnabled());
        date.addStyleName(resources.css().redoraTableInput());
        date.addStyleName(resources.css().redoraDate());

        datePanel.setWidget(0, 0, date);
        datePanel.getCellFormatter().setHorizontalAlignment(0, 0, ALIGN_LEFT);

        calendar = new Image("images/date.png");
        calendar.setStyleName(resources.css().redoraDateCalendar());
        datePanel.setWidget(0, 1, calendar);
        datePanel.getCellFormatter().setHorizontalAlignment(0, 1, ALIGN_LEFT);

        setName(name);
        getElement().setId(id);
        bind();
    }

    private void bind() {
        calendar.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (date.isEnabled()) {
                    final PopupPanel popupPanel = new PopupPanel();
                    popupPanel.setAnimationEnabled(true);
                    popupPanel.setPopupPosition(event.getClientX(), event.getClientY());
                    popupPanel.setAnimationEnabled(true);
                    popupPanel.setAutoHideEnabled(true);
                    FlowPanel panel = new FlowPanel();
                    popupPanel.setWidget(panel);

                    DatePicker datePicker = new DatePicker();
                    datePicker.setValue(getValue());
                    datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
                        public void onValueChange(ValueChangeEvent<Date> event) {
                            setValue(event.getValue());
                            popupPanel.hide();
                        }
                    });
                    panel.add(datePicker);

                    popupPanel.show();
                }
            }
        });
    }

    public boolean isEnabled() {
        return date.isEnabled();
    }

    public void setEnabled(boolean enabled) {
        date.setEnabled(enabled);
        calendar.setVisible(enabled);
    }


    @Override
    public void setName(String name) {
        date.setName(name);
    }

    @Override
    public String getName() {
        return date.getName();
    }

    @Override
    public Date getValue() {
        if (date.getValue() == null || date.getValue().isEmpty()) {
            return null;
        }
        return format.parse(date.getValue());
    }

    @Override
    public void setValue(Date value) {
        if (value == null) {
            date.setValue(null);
        } else {
            date.setValue(format.format(value));
        }
    }

    @Override
    public void setValue(Date value, boolean fireEvents) {

    }

    @Override
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> dateValueChangeHandler) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
